﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using RG.Common;
using RG.Common.Configuration;
using RG.Common.Export;
using RG.Common.WPF;
using RG.Common.WPF.BaseFrame;
using RG.Common.WPF.Filters;
using RG.Common.WPF.Filters.ToolBarFilters;
using RG.Common.WPF.Workplaces;
using RG.Consts;
using RealtorGuide.Classes;

namespace RG.Frames
{
    [SupportDataBinding]
    public class ApartmentsFrameVM : 
        BaseListFrameVM<ApartmentsFrameVM,Apartment,Apartment,IApartmentService,ApartmentsQueryParams>,
        IFrameForMultiWorkplace 
    {
        [InjectionConstructor]
        public ApartmentsFrameVM(IUnityContainer cfg)
            : base(cfg)
        {
            Name = "Квартиры";
        }


        [CollectSettings]
        public FilterStringFrameVM ManagerFilter { get; set; }

        [CollectSettings]
        public FilterStringFrameVM AddressFilter { get; set; }

        [CollectSettings]
        public FilterNewMultiStateFrameBaseVM<int, string> CountOfRoomsFilter { get; set; }

        [CollectSettings]
        public FilterNewMultiStateFrameBaseVM<AreasOfTheCity, string> AreaFilter { get; set; }

        [CollectSettings]
        public FilterStringFrameVM MicroAreaFilter { get; set; }

        [CollectSettings]
        public FilterIntervalDecFrameVM CostFilter { get; set; }

        [CollectSettings]
        public FilterIntervalDecFrameVM SquareFilter { get; set; }

        [CollectSettings]
        public FilterIntervalIntFrameVM NumberOfStoreysFilter { get; set; }

        [CollectSettings]
        public FilterIntervalIntFrameVM FloorFilter { get; set; }

        [CollectSettings]
        public FilterNewMultiStateFrameBaseVM<TypeOfMaterial, string> MaterialFilter { get; set; }

        [CollectSettings]
        public FilterDateFrameVM DateFilter { get; set; }

        [CollectSettings]
        public FilterNewMultiStateFrameBaseVM<ApartmentState, string> StateFilter { get; set; }

        protected override void InitFilters(ObservableCollection<FilterBaseVM> filters)
        {
            ManagerFilter = new FilterStringFrameVM(Cfg)
                                {
                                    Name = "Менеджер"
                                };

            AddressFilter = new FilterStringFrameVM(Cfg)
            {
                Name = "Адрес"
            };
            CountOfRoomsFilter = new FilterNewMultiStateFrameBaseVM<int, string>(Cfg)
            {
                Name = "Количество комнат",
                ItemDictionary = new[]{1,2,3,4,5,6,7,8,9,10}.ToDictionary(x => x, y => string.Format("{0}-комнатная",y))
            };
            
            AreaFilter = new FilterNewMultiStateFrameBaseVM<AreasOfTheCity, string>(Cfg)
                             {
                                 Name = "Район",
                                 ItemDictionary = Enum.GetValues(typeof(AreasOfTheCity)).ToList<AreasOfTheCity>().ToDictionary(x => x,y=>y.ToString())
                             };
            MicroAreaFilter = new FilterStringFrameVM(Cfg)
                                  {
                                      Name = "Микрорайон"
                                  };

            CostFilter = new FilterIntervalDecFrameVM(Cfg)
                             {
                                 Name = "Цена,р"
                             };

            SquareFilter = new FilterIntervalDecFrameVM(Cfg)
                            {
                                Name = "Площадь"
                            };

            FloorFilter = new FilterIntervalIntFrameVM(Cfg)
            {
                Name = "Этаж"
            };

            NumberOfStoreysFilter = new FilterIntervalIntFrameVM(Cfg)
            {
                Name = "Этажность"
            };

            MaterialFilter = new FilterNewMultiStateFrameBaseVM<TypeOfMaterial, string>(Cfg)
            {
                Name = "Материал",
                ItemDictionary = Enum.GetValues(typeof(TypeOfMaterial)).ToList<TypeOfMaterial>().ToDictionary(x => x, y => y.ToString())
            };

            DateFilter = new FilterDateFrameVM(Cfg)
            {
                Name = "Дата",
                NLastDays = 2,
                FromDate = DateTime.Now.Date,
                ToDate = DateTime.Now.Date
            };

            StateFilter = new FilterNewMultiStateFrameBaseVM<ApartmentState, string>(Cfg)
            {
                Name = "Состояние",
                ItemDictionary = Enum.GetValues(typeof(ApartmentState)).ToList<ApartmentState>().ToDictionary(x => x, y => y.ToString())
            }; 

            filters.AddRange(new List<FilterBaseVM>
                                 {
                                     ManagerFilter,
                                     AreaFilter,
                                     MicroAreaFilter,
                                     AddressFilter,
                                     CountOfRoomsFilter,
                                     SquareFilter,
                                     FloorFilter,
                                     NumberOfStoreysFilter,
                                     MaterialFilter,
                                     CostFilter,
                                     StateFilter,
                                     DateFilter,
                                 });

        }

        protected override void InitColumns(ObservableCollection<IColumnInfo> columns)
        {
            columns.AddRange(new List<IColumnInfo>()
                                 {
                                     new ColumnInfoInt<Apartment>("Код", 50, x => x.Id),
                                     new ColumnInfoString<Apartment>("Менеджер", 150, x => x.Manager.FIO),
                                     new ColumnInfoString<Apartment>("Район", 150, x => x.Area.ToString()),
                                     new ColumnInfoString<Apartment>("Микрорайон", 150, x => x.MicroArea.Name),
                                     new ColumnInfoString<Apartment>("Адрес", 200, x => x.Address),
                                     new ColumnInfoInt<Apartment>("Количество комнат", 100, x => x.CountOfRooms),
                                     new ColumnInfoNum<Apartment>("Площадь", 100, x => x.Square),
                                     new ColumnInfoInt<Apartment>("Этаж", 100, x => x.Floor),
                                     new ColumnInfoInt<Apartment>("Этажность", 100, x => x.NumberOfStoreys),
                                     new ColumnInfoString<Apartment>("Материал", 100, x => x.TypeOfMaterial.ToString()),
                                     new ColumnInfoNum<Apartment>("Цена", 100, x => x.Cost),
                                     new ColumnInfoString<Apartment>("Состояние", 100, x => x.State.ToString()),
                                 });
        }

        protected override void InitButtons(ObservableCollection<IToolbarButton> buttons)
        {
            buttons.AddRange(new List<IToolbarButton>
                       {
                           new RefreshButton(Refresh),
                           new AddButton(Add),
                           new EditButton(Edit),
                           new DeleteButton(Sold)
                               {
                                   Header = "Продано"
                               },
                       });
        }

        private void Add()
        {
              
        }

        private void Sold()
        {
            
        }

        private void Edit()
        {
            
        }

       

        protected override ApartmentsQueryParams InitQueryParam()
        {
            return new ApartmentsQueryParams()
                       {
                           ManagerName = ManagerFilter.Value,
                           Address = AddressFilter.Value,
                           Rooms = CountOfRoomsFilter.Value,
                           Areas = AreaFilter.Value,
                           MicroAreaName = MicroAreaFilter.Value,
                           Materials = MaterialFilter.Value,
                           Date = DateFilter.Value,
                           States = StateFilter.Value,
                           Floor = FloorFilter.Value,
                           NumberOfStoreys = NumberOfStoreysFilter.Value,
                           Cost = CostFilter.Value,
                           Square = SquareFilter.Value
                       };
        }

        protected override Apartment fromDTO(Apartment dto)
        {
            return dto;
        }

        public bool CanClose
        {
            get { return true; }
        }

        public string Name { get; set; }
    }
}
